package com.mew.slots.core.game.klxg.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.entity.game.WinFree;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.hdcz.bo.HdczInRoom;
import com.mew.slots.core.game.hdcz.message.HdczResponseMessage;
import com.mew.slots.core.game.hdcz.roller.HdczXml;
import com.mew.slots.core.game.hdcz.room.HdczRoom;
import com.mew.slots.core.game.klxg.bo.KlxgInRoom;
import com.mew.slots.core.game.klxg.bo.KlxgPlayMsgs;
import com.mew.slots.core.game.klxg.message.KlxgResponseMessage;
import com.mew.slots.core.game.klxg.roller.KlxgIli;
import com.mew.slots.core.game.klxg.roller.KlxgXml;
import com.mew.slots.core.game.klxg.room.KlxgRoom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Dylin
 */
@Service
@Slf4j
public class KlxgService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {
        KlxgInRoom in = new KlxgInRoom();
        if (StringUtils.isNotEmpty(player.getWinFree().getJson())){
            try {
                in = JSON.parseObject(player.getWinFree().getJson(), KlxgInRoom.class);
            }catch (Exception e){
                log.debug("WinFree json解析出错：{}",player.getWinFree().getJson());
            }
        }
        KlxgRoom room = new KlxgRoom(in,player.getWinFree().getBounsChange()>0);
        room.init(player.getRollerId(), KlxgIli.getInstance(),in,player.getWinFree().getFrees()>0,player.getInBetNumber(),player.getControlType());
        room.roundPlayIli();
        BasePlayMsgs res = room.getRes();
        if(res.getFree()==0&&player.getWinFree().getFrees()>0&&player.getWinFree().getFreeNumber()==1){
            for (int i = 0; i < in.getLock().length; i++) {
                in.getLock()[i]=0;
            }
        }
        player.getWinFree().setJson(JSON.toJSONString(in));
        return res;
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        KlxgResponseMessage message = new KlxgResponseMessage();
        KlxgPlayMsgs pmres = (KlxgPlayMsgs) res;
        message.setChange(res.isChange()?1:0);
        message.setLock(pmres.getLock());
        return message;
    }
    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) {
        KlxgResponseMessage ms = (KlxgResponseMessage) message;
        ms.setFreeCount(winFree.getFreeCount());
    }
}
